package cn.chenyu.util;

import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.Map;
//import java.util.Objects;

import static org.apache.log4j.Logger.getLogger;

public class UrlUtil {
    private final static String encoding = "utf-8";
    private  static final Logger log  = getLogger(UrlUtil.class);

    @Test
    public void Test() throws Exception{
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 定义请求URL和参数，键值对
        URI uri = new URIBuilder("http://47.106.10.148/AppInfoSystem/bankend/flatform/login.do").setParameter("userCode",
                "admin").addParameter("userPassword","123456").build();
        //选择请求方式
        //HttpGet httpGet = new HttpGet(uri);

        HttpPost httpPost = new HttpPost(uri);
        // 伪装浏览器请求(设置网页头部head)
        httpPost.setHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36");
        //执行请求操作，并拿到结果（同步阻塞）获取response-响应
        CloseableHttpResponse response = httpclient.execute(httpPost);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        String body = "";
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);
//            System.out.println("--"+response.getStatusLine());
            System.out.println("body"+body);
            //TxtMofify.WriteToLocal("E:\\img\\test.txt",entity.getContent());
        }
        //销毁
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        System.out.println("结束");
    }

    public static CloseableHttpResponse getCloseableHttpResponse(String url , Map params) throws Exception{
        URI uri = null;
        if(params != null && params.size() > 1 )
            uri = new URIBuilder(url).addParameters(CollectionUtil.MapConversionNVP(params)).build();
        else
            uri = new URIBuilder(url).build();
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(uri);
        // 伪装浏览器请求
        httpPost.setHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36");
        return httpclient.execute(httpPost);
    }
    @Test
    public void tadsa() throws Exception{
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String url = "https://tieba.baidu.com/p/5587497307";
        URI uri = new URIBuilder(url).build();
//        HttpPost httpPost = new HttpPost(uri);
        HttpGet httpGet = new HttpGet(uri);
        // 伪装浏览器请求
        CloseableHttpResponse response = httpclient.execute(httpGet);
        String srtResult = "";
        if(response.getStatusLine().getStatusCode() == 200) {
            srtResult = EntityUtils.toString(response.getEntity());//获得返回的结果
            System.out.println(StringUtil.ConverStringCoding(srtResult,"UTF-8"));

        }
        httpGet.clone();
        response.close();
    }
}
